package com.jojo.jojoWorld.service.impl;

import com.jojo.jojoWorld.entity.ZwDeptRelation;
import com.jojo.jojoWorld.mapper.TreeMapper;
import com.jojo.jojoWorld.service.TreeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * treeServiceImpl
 *
 * @author byc
 * @date 2022-02-22
 */
@Service
public class TreeServiceImpl implements TreeService {

    @Resource
    private TreeMapper treeMapper;

    @Override
    public List<ZwDeptRelation> getAll() {
        return treeMapper.getAll();
    }

    @Override
    public List<ZwDeptRelation> getTree() {
        List<ZwDeptRelation> listAll = treeMapper.getAll();

        Set<ZwDeptRelation> treeList = new HashSet<>();

        for (ZwDeptRelation zwDeptRelation : listAll) {
            List<ZwDeptRelation> childList;
            childList = listAll.stream().map(o -> {
                if (o.getParentId().equals(zwDeptRelation.getId())){
                    return o;
                } else {return null;}
            }).collect(Collectors.toList());
            zwDeptRelation.setChild(childList);
            treeList.add(zwDeptRelation);
        }
        return null;
    }

    public List<ZwDeptRelation> getChild(){
        return null;
    }

}
